local skel = fk.CreateSkill {
  name = "efengqi__guanxing",
}

Fk:loadTranslationTable{
  ["efengqi__guanxing"] = "观星",
  [":efengqi__guanxing"] = "准备阶段，你可以观看牌堆顶五张牌，将这些牌以任意顺序放回牌堆顶或牌堆底，然后可以令一名其他角色于此后第X个回合内获得“大雾”效果（X为置底牌数）",

  ["#efengqi__guanxing-choose"] = "观星：可以令一名其他角色于此后第 %arg 个回合内获得“大雾”",
  ["@efengqi__guanxing_dawu"] = "大雾倒时:",

  ["$efengqi__guanxing1"] = "观今夜天象，知天下大事。",
  ["$efengqi__guanxing2"] = "知天易，逆天难。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target == player and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local result = room:askToGuanxing(player, { cards = room:getNCards(5), skill_name = skel.name})
    local num = #result.bottom
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player, false), min_num = 1, max_num = 1,
      prompt = "#efengqi__guanxing-choose:::"..num, skill_name = skel.name
    })
    if #tos > 0 then
      room:addSkill("efengqi__dawu")
      local to = tos[1]
      if num == 0 then -- 本回合立即获得大雾
        room:addTableMark(to, "@@efengqi__dawu", "-turn")
      else -- 延时获得大雾
        local mark = to:getTableMark("@efengqi__guanxing_dawu")
        if table.insertIfNeed(mark, num) then
          room:setPlayerMark(to, "@efengqi__guanxing_dawu", mark)
        end
      end
    end
  end,
})

skel:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@efengqi__guanxing_dawu") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = {}
    for _, num in ipairs(player:getMark("@efengqi__guanxing_dawu")) do
      if num == 1 then
        player.room:addTableMark(player, "@@efengqi__dawu", "-turn")
      else
        table.insert(mark, num - 1)
      end
    end
    player.room:setPlayerMark(player, "@efengqi__guanxing_dawu", #mark > 0 and mark or 0)
  end,
})

return skel
